﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Farkhod.QuickDataViewer
{
    /// <summary>
    /// Interaction logic for ConnectionStringEditor.xaml
    /// </summary>
    public partial class ConnectionStringEditor : Window
    {
        public ConnectionStringEditor()
        {
            InitializeComponent();
            LoadConnectionStringValues();
        }
        // String _ConnString = GetConnString();

        public string GetConnString()
        {
            if (rbSQLAuth.IsChecked == true)
                return BuildConnectionString(txtServer.Text.Trim(), txtDBName.Text.Trim(), txtDBLogin.Text.Trim(), txtDBPassword.Text.Trim(), true);
            else
                return BuildConnectionString(txtServer.Text.Trim(), txtDBName.Text.Trim(), txtDBLogin.Text.Trim(), txtDBPassword.Text.Trim(), false);
        }

        private string BuildConnectionString(string ServerAddress, string DBName, string userName, string userPassword, bool sqlAuthentication)
        {
            try
            {
                string connectString = string.Empty;
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectString);
                builder.DataSource = ServerAddress.Trim();
                builder.InitialCatalog = DBName.Trim();
                if (sqlAuthentication)
                {
                    builder.UserID = userName.Trim();
                    builder.Password = userPassword.Trim();
                }
                else
                {
                    builder.IntegratedSecurity = true;
                    builder.PersistSecurityInfo = false;
                }

                return builder.ConnectionString;

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private void LoadConnectionStringValues()
        {
            txtServer.Text = DBManager.Instance.ConnectionStringBuilder.DataSource;
            txtDBName.Text = DBManager.Instance.ConnectionStringBuilder.InitialCatalog;
            txtDBLogin.Text = DBManager.Instance.ConnectionStringBuilder.UserID;
            txtDBPassword.Text = DBManager.Instance.ConnectionStringBuilder.Password;
            bool isTrustedConnection = false;
            bool.TryParse(DBManager.Instance.ConnectionStringBuilder["Trusted_Connection"].ToString(), out isTrustedConnection);
            if (isTrustedConnection || DBManager.Instance.ConnectionStringBuilder.IntegratedSecurity)
            {
                rbWinAuth.IsChecked = true;
                rbSQLAuth.IsChecked = false;
            }
            else
            {
                rbWinAuth.IsChecked = false;
                rbSQLAuth.IsChecked = true;
            }
        }

        private void rbWinAuth_Checked(object sender, RoutedEventArgs e)
        {
            if (rbWinAuth.IsChecked == true)
            {
                lblLogin.IsEnabled = false;
                lblPass.IsEnabled = false;
                txtDBLogin.IsEnabled = false;
                txtDBPassword.IsEnabled = false;
            }
            else
            {
                lblLogin.IsEnabled = true;
                lblPass.IsEnabled = true;
                txtDBLogin.IsEnabled = true;
                txtDBPassword.IsEnabled = true;
            }
        }

        private void rbSQLAuth_Checked(object sender, RoutedEventArgs e)
        {
            if (rbSQLAuth.IsChecked == true)
            {
                if (lblLogin != null)
                    lblLogin.IsEnabled = true;

                if (lblPass != null)
                    lblPass.IsEnabled = true;

                if (txtDBLogin != null)
                    txtDBLogin.IsEnabled = true;

                if (txtDBPassword != null) 
                    txtDBPassword.IsEnabled = true;
            }
            else
            {
                if (lblLogin != null)
                    lblLogin.IsEnabled = false;

                if (lblPass != null)
                    lblPass.IsEnabled = false;

                if (txtDBLogin != null)
                    txtDBLogin.IsEnabled = false;

                if (txtDBPassword != null) 
                    txtDBPassword.IsEnabled = false;
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
           string modifiedConnString = GetConnString();
           DBManager.ConnectionString = modifiedConnString;
           DBManager.Instance.ConnectionStringBuilder = new SqlConnectionStringBuilder(modifiedConnString);
           DBManager.Instance.UpdateConnectionStringInConfigFile(modifiedConnString);
           this.DialogResult = true;
           this.Close();
        }

    }

    

}
